package com.manger.listener;

import com.alibaba.fastjson.JSONObject;
import com.manger.chrome.DriverManger;
import com.manger.thread.HeartBeatThread;
import com.manger.utils.DataTools;
import com.manger.utils.RemoteExecute;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * @ClassName ChromeCLickListener
 * @Description TODO
 * @Author @LiuHongJun
 * @Date 2025-10-27 11:09
 * @Version 1.0
 */
public class ChromeCLickListener implements MouseListener {


    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println("++++++++++++++++++打开浏览器++++++++++++++++++");
        HeartBeatThread heartBeatThread = new HeartBeatThread();
        try{
            if(DataTools.hasUseProxy ){
                String url = DataTools.baseUrl + "/proxy/getCanUseProxyIp?loginUserId=" + DataTools.loginUserId;
                JSONObject jsonObject = RemoteExecute.execute(url);
                if(StringUtils.isEmpty(jsonObject.getString("ip"))){
                    JOptionPane.showMessageDialog(null, "没有获取的代理Ip，请联系管理员!", "警告", JOptionPane.WARNING_MESSAGE);
                    return;
                }

                DataTools.useProxyId = jsonObject.getString("id");

                heartBeatThread.start();

                DriverManger.init(jsonObject.getString("ip"),
                        jsonObject.getIntValue("port"));

            }else {
                DriverManger.init();
            }

        }catch (Exception ex){
            ex.printStackTrace();
            if(DriverManger.driver != null){
                System.out.println("=========chromeClickListener=========");
                DriverManger.driver.quit();
            }
            heartBeatThread.interrupt();
        }finally {

        }

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
